package com.jhgsys.internal.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jhgsys.internal.base.dto.BaseCautionaryLessonDto;
import com.jhgsys.internal.base.entity.BaseCautionaryLesson;
import com.jhgsys.internal.base.mapper.BaseCautionaryLessonMapper;
import com.jhgsys.internal.base.service.IBaseCautionaryLessonService;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zhangwei
 * @date 2024-09-05 09:48:17
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseCautionaryLessonServiceImpl extends ServiceImpl<BaseCautionaryLessonMapper, BaseCautionaryLesson> implements IBaseCautionaryLessonService {

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     * @param request QueryRequest
     * @param baseCautionaryLesson baseCautionaryLesson
     * @return IPage<BaseCautionaryLesson>
    */
    @Override
    public IPage<BaseCautionaryLesson> findBaseCautionaryLessonPage(QueryRequest request, BaseCautionaryLesson baseCautionaryLesson) {
        LambdaQueryWrapper<BaseCautionaryLesson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCautionaryLesson::getDeletedFlg, 0);
        // TODO 设置查询条件
        if(StringUtils.isNotBlank(baseCautionaryLesson.getTenantId())){
            queryWrapper.like(BaseCautionaryLesson::getTenantId,baseCautionaryLesson.getTenantId());
        }
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionaryTitle())){
            queryWrapper.like(BaseCautionaryLesson::getCautionaryTitle,baseCautionaryLesson.getCautionaryTitle());
        }
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionaryDept())){
            queryWrapper.like(BaseCautionaryLesson::getCautionaryDept,baseCautionaryLesson.getCautionaryDept());
        }
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionarySource())){
            queryWrapper.eq(BaseCautionaryLesson::getCautionarySource,baseCautionaryLesson.getCautionarySource());
        }
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionaryDeptId())){
            queryWrapper.eq(BaseCautionaryLesson::getCautionaryDeptId,baseCautionaryLesson.getCautionaryDeptId());
        }

        Page<BaseCautionaryLesson> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     * @param baseCautionaryLesson baseCautionaryLesson
     * @return List<BaseCautionaryLesson>
     */
    @Override
    public List<BaseCautionaryLesson> findBaseCautionaryLessonList(BaseCautionaryLesson baseCautionaryLesson) {
	    LambdaQueryWrapper<BaseCautionaryLesson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCautionaryLesson::getDeletedFlg, 0);
		// TODO 设置查询条件
        if(StringUtils.isNotBlank(baseCautionaryLesson.getTenantId())){
            queryWrapper.like(BaseCautionaryLesson::getTenantId,baseCautionaryLesson.getTenantId());
        }
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionaryTitle())){
            queryWrapper.like(BaseCautionaryLesson::getCautionaryTitle,baseCautionaryLesson.getCautionaryTitle());
        }
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionaryDept())){
            queryWrapper.like(BaseCautionaryLesson::getCautionaryDept,baseCautionaryLesson.getCautionaryDept());
        }
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionarySource())){
            queryWrapper.eq(BaseCautionaryLesson::getCautionarySource,baseCautionaryLesson.getCautionarySource());
        }
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionaryDeptId())){
            queryWrapper.eq(BaseCautionaryLesson::getCautionaryDeptId,baseCautionaryLesson.getCautionaryDeptId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-09-05 09:48:17
    * @param id
    * @return BaseCautionaryLesson
    */
    @Override
    public BaseCautionaryLessonDto findBaseCautionaryLessonById(String id){
        BaseCautionaryLesson baseCautionaryLesson = this.baseMapper.selectById(id);
        BaseCautionaryLessonDto baseCautionaryLessonDto = BeanHelper.copyProperties(baseCautionaryLesson, BaseCautionaryLessonDto.class);
        if(StringUtils.isNotBlank(baseCautionaryLesson.getCautionaryFileInfo())){
            baseCautionaryLessonDto.setUploadFileInfo(JSONObject.parseObject(baseCautionaryLesson.getCautionaryFileInfo()));
        }
        return baseCautionaryLessonDto;
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     * @param baseCautionaryLesson baseCautionaryLesson
     */
    @Override
    @Transactional
    public void createBaseCautionaryLesson(BaseCautionaryLesson baseCautionaryLesson) {
        baseCautionaryLesson.setCreatedTime(new Date());
        this.save(baseCautionaryLesson);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     * @param baseCautionaryLesson baseCautionaryLesson
     */
    @Override
    @Transactional
    public void updateBaseCautionaryLesson(BaseCautionaryLesson baseCautionaryLesson) {
        baseCautionaryLesson.setUpdatedTime(new Date());
        this.saveOrUpdate(baseCautionaryLesson);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-09-05 09:48:17
     * @param baseCautionaryLesson baseCautionaryLesson
     * @param baseCautionaryLessonIds baseCautionaryLessonIds
     */
    @Override
    @Transactional
    public void deleteBaseCautionaryLesson(BaseCautionaryLesson baseCautionaryLesson,String baseCautionaryLessonIds) {
        List<String> list = Arrays.asList(baseCautionaryLessonIds.split(StringPool.COMMA));
        this.baseMapper.update(baseCautionaryLesson,new LambdaQueryWrapper<BaseCautionaryLesson>().in(BaseCautionaryLesson::getCautionaryLessonId,list));
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-09-05 09:48:17
    * @param baseCautionaryLessonIds baseCautionaryLessonIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseCautionaryLesson(String baseCautionaryLessonIds) {
        List<String> list = Arrays.asList(baseCautionaryLessonIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseCautionaryLesson> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseCautionaryLesson::getCautionaryLessonId,list);
        this.remove(wapper);
    }

    /**
     * 其他业务模块调用，保存警示信息
     *
     * @param tenantId
     * @param source          警示来源 1:事故管理 2：违章记分 3：隐患上报 4：安全检查
     * @param sourceId        业务数据主键id
     * @param sourceTitle     标题
     * @param cautionDeptId   需警示部门id
     * @param cautionDeptName 需警示部门名称
     * @param createBy 创建人
     */
    @Override
    public FebsResponse saveCautionaryLessonFromOthers(String tenantId, String source, String sourceId, String sourceTitle, String cautionDeptId, String cautionDeptName, String createBy){
        if(StringUtils.isBlank(tenantId)){
            return new FebsResponse().fail().message("未提供tenantId");
        }
        if(StringUtils.isBlank(source)){
            return new FebsResponse().fail().message("未提供警示来源");
        }
        if(StringUtils.isBlank(sourceId)){
            return new FebsResponse().fail().message("未提供业务数据主键id");
        }
        if(StringUtils.isBlank(sourceTitle)){
            return new FebsResponse().fail().message("未提供标题");
        }
        if(StringUtils.isBlank(cautionDeptId)){
            return new FebsResponse().fail().message("未提供需警示部门id");
        }
        BaseCautionaryLesson cautionaryLesson = new BaseCautionaryLesson();
        cautionaryLesson.setTenantId(tenantId);
        cautionaryLesson.setCautionarySource(source);
        cautionaryLesson.setCautionarySourceId(sourceId);
        cautionaryLesson.setCautionaryTitle(sourceTitle);
        cautionaryLesson.setCautionaryDeptId(cautionDeptId);
        cautionaryLesson.setCautionaryDept(cautionDeptName);
        cautionaryLesson.setStatus("0");
        cautionaryLesson.setCreatedBy(createBy);
        cautionaryLesson.setCreatedTime(new Date());
        cautionaryLesson.setDeletedFlg("0");
        this.save(cautionaryLesson);
        return new FebsResponse().success().message("添加警示成功");
    }

}
